package com.pactera.madp.cp.controller.oms.contactfield;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.contactfield.ContactFieldDto;
import com.pactera.madp.cp.api.dto.contactfield.BatchUpdateContactFieldDto;
import com.pactera.madp.cp.api.entity.ContactFieldEntity;
import com.pactera.madp.cp.api.entity.ContactFieldTypeEntity;
import com.pactera.madp.cp.api.vo.ApiRespVO;
import com.pactera.madp.cp.api.vo.ContactFieldVO;
import com.pactera.madp.cp.common.util.wm.ApiRespUtils;
import com.pactera.madp.cp.service.ContactFieldTypeService;
import com.pactera.madp.cp.service.IContactFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhaojinjian
 * @ClassName ContactFieldController.java
 * @Description TODO
 * @createTime 2020/12/16 11:58
 */

@RestController
@AllArgsConstructor
@RequestMapping("/contactfield")
@Api(value = "contactfield", tags = "高级属性管理")
public class ContactFieldController {

    private IContactFieldService contactFieldService;
    private ContactFieldTypeService contactFieldTypeService;

    @GetMapping("/portrait")
    @ApiOperation(value = "客户画像")
    @ApiImplicitParams({
            @ApiImplicitParam(name="fieldId",value="属性id",dataType="Integer"),
            @ApiImplicitParam(name="name",value="客户名",dataType="String")
    })
    public ApiRespVO getPortrait(Integer fieldId, String name) {
        return ApiRespUtils.getApiRespOfOk(contactFieldService.getPortrait(fieldId, name));
    }

    /**
     * @description 查看高级属性
     * @author zhaojinjian
     * @createTime 2020/12/16 15:00
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查看高级属性")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="属性id",dataType="Integer")
    })
    public R<ContactFieldVO> showContactField(@PathVariable Integer id) {
        return R.ok(contactFieldService.getContactFieldInfo(id));
    }

    /**
     * @description 获取高级属性的集合列表
     * @author zhaojinjian
     * @createTime 2020/12/16 15:03
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取高级属性的集合列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value="属性状态，2--全部  1--开启  0--关闭",dataType="Integer")
    })
    public R<Page<List<ContactFieldEntity>>> getContactFieldList(Integer status, Page page) {
        if (status != null && status > 2) {
            throw new CheckedException("非法的参数");
        }
        return R.ok(contactFieldService.getContactFieldList(status, page));
    }

    /**
     * @description 新增高级属性
     * @author zhaojinjian
     * @createTime 2020/12/16 15:05
     */
    @SysLog("新增高级属性")
    @PostMapping
    @ApiOperation(value = "新增高级属性")
    public R<Boolean> insertContactField(@RequestBody ContactFieldDto parem) {
        return contactFieldService.insertContactField(parem) ? R.ok(Boolean.TRUE,"新增成功") : R.failed(Boolean.FALSE,"新增失败") ;
    }

    /**
     * @description 修改高级属性
     * @author zhaojinjian
     * @createTime 2020/12/16 15:06
     */
    @SysLog("修改高级属性")
    @PutMapping
    @ApiOperation(value = "修改高级属性")
    public R<Boolean> updateContactField(@RequestBody ContactFieldDto parem) {
        return contactFieldService.updateContactField(parem)  ? R.ok(Boolean.TRUE,"修改成功") : R.failed(Boolean.FALSE,"修改失败") ;
    }

    /**
     * @description 删除高级属性
     * @author zhaojinjian
     * @createTime 2020/12/16 15:09
     */
    @SysLog("删除高级属性")
    @DeleteMapping("/{fieldId}")
    @ApiOperation(value = "删除高级属性")
    @ApiImplicitParams({
            @ApiImplicitParam(name="fieldId",value="属性id",dataType="Integer")
    })
    public R<Boolean> deleteContactField(@PathVariable Integer fieldId) {
        return contactFieldService.deleteContactField(fieldId) ? R.ok(Boolean.TRUE,"删除成功"): R.failed(Boolean.FALSE,"删除失败");
    }

    /**
     * @description 批量修改/删除高级属性
     * @author zhaojinjian
     * @createTime 2020/12/16 15:11
     */
    @SysLog("批量修改高级属性")
    @PutMapping("/batchupdate")
    @ApiOperation(value = "批量修改高级属性")
    public R<Boolean> batchUpdate(@RequestBody BatchUpdateContactFieldDto parem) {
        if (parem.getUpdate() == null) {
            R.failed(Boolean.FALSE,"非法的参数");
        }
        return contactFieldService.BatchUpdateContactField(parem)? R.ok(Boolean.TRUE,"修改成功") : R.failed(Boolean.FALSE,"修改失败") ;
    }

    /**
     * @description 修改高级属性的状态
     * @author zhaojinjian
     * @createTime 2020/12/16 15:14
     */
    @SysLog("修改高级属性的状态")
    @PutMapping("/statusupdate")
    @ApiOperation(value = "修改高级属性的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="属性id",dataType="Integer"),
            @ApiImplicitParam(name="status",value="属性状态，1--开启  0--关闭",dataType="Integer")
    })
    public R<Boolean> statusUpdate(Integer id,Integer status) {
        if (id <= 0) {
            throw new CheckedException("id 不能为空");
        }
        if (status < 0 || status > 1) {
            throw new CheckedException("属性状态异常");
        }

        return contactFieldService.updateStatus(id, status) ? R.ok(Boolean.TRUE,"修改成功") : R.failed(Boolean.FALSE,"修改失败") ;
    }

    @GetMapping("/type")
    @ApiOperation(value = "获取字段类型列表")
    public R<List<ContactFieldTypeEntity>> getTypeList(){
      return R.ok(contactFieldTypeService.getList());
    }

}
